<template>
  <div class="register_container">
    <div class="register_box">
      <!-- logo区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.jpg" alt="logo" />
        <span>海报设计系统</span>
      </div>
      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" type="text" auto-complete="off" placeholder="账号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="nickname">
          <el-input v-model="registerForm.nickname" type="text" auto-complete="off" placeholder="昵称" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" auto-complete="off" placeholder="密码" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" auto-complete="off" placeholder="确认密码" prefix-icon="el-icon-lock" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%;" @click="doRegister()">注册</el-button>
        </el-form-item>
        <el-row style="text-align: center; margin-top:-10px">
          <el-link type="primary" @click="goToLogin">已有账号？点击登录</el-link>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      registerForm: {
        username: "",
        nickname: "",
        password: "",
        confirmPassword: "",
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        nickname: [
          { required: true, trigger: "blur", message: "请输入您的昵称" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请确认您的密码" },
          { validator: this.validateConfirmPassword, trigger: "blur" }
        ],
      },
    }
  },
  methods: {
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.registerForm.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    },
    doRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          // 执行注册逻辑
          console.log("注册成功");
        } else {
          console.log("表单验证失败");
          return false;
        }
      });
    },
    goToLogin() {
      this.$router.push('/login');
    }
  }
}
</script>

<style scoped>
.register_container {
  background-image: url("../assets/img.jpg");
  background-size: 100% 100%;
  height: 100%;
}

.register_box {
  width: 450px;
  height: 500px;
  background-color: white;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  overflow: hidden;
}

.avatar_box {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100px;
  width: 100%;
  padding: 10px 0;
}

.avatar_box img {
  height: 80px;
  width: 80px;
  border-radius: 50%;
  background-color: #eee;
  margin-right: 5px;
}

.avatar_box span {
  font-size: 24px;
  color: #333;
}

.register-form {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 10px 35px 15px 35px;
  background: #fff;
  text-align: left;
}
</style>
